<?php
include_once("../../php/include_all.php");
?>
<div class="widget-header widget-header-flat ">
    <h5><i class="icon-calendar"></i>Desa</h5>
</div>
<div class="position-relative" id="page-content">
    <a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>&nbsp;Tambah Data</a>
    &nbsp;<span id="load" class="spinner"></span>
</div>
<div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    <header class="modal-header">
        <a href="#" class="close btn-close" data-dismiss="modal">x</a>
        <h4>Data Desa</h4>
    </header>

    <div class="modal-body">
        <form id="form_desa" class="form-horizontal" >
            <input type="hidden" name="act" value="save_desa">
            <input type="hidden" id="id_desa" name="id_desa" value="0">
            <table class="table-form">
                <tbody>
                <div class="control-group">
                    <label class="control-label" for="kec">Nama Kecamatan :</label>
                    <div class="controls">  
                        <select name="id_kecamatan">
                            <option id="id_kec" selected value=0>-Pilih Satu-</option>
                            <?php
                            $kecamatan = get_datas("select * from mus_kecamatan ");
                            foreach ($kecamatan as $kecamatan) {
                                ?>
                                <option value="<?php echo $kecamatan['id_kecamatan']; ?>"><?php echo $kecamatan['nama_kecamatan']; ?></option>
                                <?php
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="des">Nama Desa :</label>
                    <div class="controls">  
                        <input type="text" id="nama_desa" class="span3" placeholder="nama desa..." name="nama_desa" ></br></br>
                        <a class="btn btn-primary2 btn-small bt-simpan-desa"><div id="loader" style="margin-top:2px"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>
                        <span id="result"></span>
                    </div>
                </div>
                </tbody>
            </table>
        </form>
    </div>
</div>
<div class="box-content">
    <table class="table table-bordered geo-table">
        <thead>
            <tr>
                <th class="center" width="2%" >No</th>
                <th class="center" width="20%">Nama Kecamatan</td>
                <th class="center" width="58%">Nama Desa</td>
                <th class="center" width="25%">Action</td>
            </tr>
        </thead>
        <tbody id="tbody_desa">
            <?php
            $x = 1;
            $desa = get_datas("select a.nama_kecamatan,b.iddesa,b.nama_desa from mus_kecamatan a, mus_desa b where a.id_kecamatan=b.id_kecamatan order by nama_desa");
            foreach ($desa as $desa) {
                ?>
                <tr>
                    <td><?php echo $x ?></td>
                    <td><?php echo $desa['nama_kecamatan']; ?></td>
                    <td><?php echo $desa['nama_desa']; ?></td>
                    <td>
                        <a href="#modalwin" data-toggle="modal"  class="btn btn-small btn-info bt-edit-desa" name="<?php echo $desa['iddesa']; ?>"><i class="icon-edit icon-white"></i> ubah</a>
                        <a class="btn btn-danger btn-small bt-delete-desa" name="<?php echo $desa['iddesa']; ?>"><i class="icon-trash icon-white"></i> hapus</a>
                    </td>
                </tr>
                <?php
                $x++;
            }
            ?>
        </tbody>
    </table>
</div>
<script>
    var urls = 'admin/master_desa/desa_save.php';

    $('.bt-simpan-desa').click(function(){
        var btn  = $(this);
        var load = $('#loader');
        var rslt = $('#result');
		
       load.addClass('icon-spinner icon-spin icon-2x white');
     //   btn.removeClass('btn-primary').addClass('btn-info');
	
        var form = $('#form_desa');
        var data = form.serializeArray();
        var post = $.post(urls,data);
        post.done(function(res){
            var result = res.split('__');
            //   0      1     2
            //success__iddesa__(isi tbody)tr
            if(result.length==3){
                if(result[0]=='success'){
                    rslt.html('<font color="green">tersimpan...</font>');
                    setTimeout(function(){
                        rslt.html('');
                    },1500);
				
                    var tbody = $('#tbody_desa');
                    tbody.html(result[2]);
					 init();
					$('#id_desa').val('0');
                    $('#nama_desa').val(' ');
                    document.getElementById('id_kec').selected=true;
                   $('#modalwin').modal('hide');
				
                }else{
                    rslt.html('<font color="red">'+res+'</font>');
                }
            }else{
                rslt.html('<font color="red">'+res+'</font>');
            }
            load.removeClass();
            btn.removeClass('btn-info').addClass('btn-primary');
        });
    });
	
	$('.btn-tambah').click(function(){
		$('#id_desa').val('0');
        $('#nama_desa').val(' ');
        document.getElementById('id_kec').selected=true;
	});
	
    function init(){
        $('.bt-delete-desa').click(function(){
            var btn  = $(this);
            var pid  = this.name;
            var load = $(this);
            load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
            bootbox.confirm("Anda akan menghapus desa ini?", function(result) {
                if(result==true){
                    if(pid!=0){
                        var post = $.post(urls,{act:"del_desa",id_desa:pid});
                        post.done(function(data){
						
						var hasil = data.split('__');
							if(hasil.length==2){
								if(hasil[0]=='success'){
							   //     btn.parent().parent().remove();
									var tbody = $('#tbody_desa');
									tbody.html(hasil[1]);
									init();
								}
							}
                        });
                        load.html('<i class="icon-trash icon-white"></i> hapus');
                    }
                }else{
                    load.html('<i class="icon-trash icon-white"></i> hapus');
                }
            });
        });
	
        $('.bt-edit-desa').click(function(){
		    var id_desa= this.name;
            var form = $('#form_desa');
            var post = $.post(urls,{act:'edit_desa',id_desa:id_desa});
            post.done(function(res){
                var value = res.split('__');
				$('#id_desa').val(value[0]);
                form.find('select[name=id_kecamatan]').val(value[1]);
                form.find('input[name=nama_desa]').val(value[2]);
            });
        });
    }

/*	
$('.btn-close').click(function(){
	$('#id_desa').val(0);
	document.getElementById('form_desa').reset();
});*/
    init();
</script>